package gui;

import gui.Machine.MachineListFrame;
import gui.RepairType.RepairTypeListFrame;
import gui.Reparation.ReparationCUDialog;
import gui.Reparation.ReparationListFrame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import model.Machine;

import service.Service;

public class MainFrame extends JFrame {

	protected static SimpleDateFormat m_frm;
	private JButton btnNewRep, btnReparations, btnRepairTypes, btnMachines,
			btnMachineTypes, btnSpareParts;
	private ReparationListFrame reparationListFrame;
	private RepairTypeListFrame repairTypeListFrame;
	private MachineListFrame machineListFrame;

	private JLabel info;
	private DefaultComboBoxModel modelMachines = new DefaultComboBoxModel();;
	private JLabel lMname, lIdleTime, lResult, lTotal, lFullResult;
	private JComboBox cbxMachine;
	private JTextField txtYM;
	private JCheckBox chbYM;

	public MainFrame() {
		setTitle("Koopy");
		setBounds(340, 140, 490, 270);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);

		m_frm = new SimpleDateFormat("<hh:mm:ss a> EE MMM dd, yyyy");

		info = new JLabel();
		info.setBounds(10, 210, this.getWidth(), 24);
		info.setForeground(Color.RED);
		getContentPane().add(info);

		getButtons();
		getIdleTimeFields();
		// Refresh the Machine List with all the Reparation and calculate the
		// IdleTime again.
		this.addWindowListener(new WindowAdapter() {
			public void windowActivated(java.awt.event.WindowEvent e) {
				chbYM.setSelected(false);
				txtYM.setText("yyyy-mm");
				txtYM.setEnabled(false);
				modelMachines.removeAllElements();
				fillComboBoxModel();
			}
		});
	}

	private void getButtons() {
		JPanel pBtn = new JPanel(null);
		pBtn.setBounds(0, 0, 190, 210);
		pBtn.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED), "Lists"));
		getContentPane().add(pBtn);

		int bX = 20;
		int bY = 20;
		int bW = 100;
		int bH = 25;

		btnNewRep = new JButton("Create new Rep");
		btnNewRep.setBounds(bX, bY, 150, bH);
		btnNewRep.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ReparationCUDialog repC = new ReparationCUDialog(
						MainFrame.this, "Create a Repararion");
				// Conference conference =
				// (Conference)listofConferences.getSelectedValue();
				// repC.setConference(conference);
				repC.setVisible(true);

			}
		});
		pBtn.add(btnNewRep);

		btnReparations = new JButton("Reparations");
		btnReparations.setBounds(bX, bY + 30, bW, bH);
		btnReparations.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnReparations) {
					if (reparationListFrame == null)
						reparationListFrame = new ReparationListFrame();
					reparationListFrame.fillListReparation(Service.getService()
							.getReparations());
					reparationListFrame.setVisible(true);
				}
			}
		});
		pBtn.add(btnReparations);

		btnRepairTypes = new JButton("RepairTypes");
		btnRepairTypes.setBounds(bX, bY + 60, bW, bH);
		btnRepairTypes.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnRepairTypes) {
					if (repairTypeListFrame == null)
						repairTypeListFrame = new RepairTypeListFrame();
					repairTypeListFrame.fillListRepairType();
					repairTypeListFrame.setVisible(true);
				}

			}
		});
		pBtn.add(btnRepairTypes);

		btnMachines = new JButton("Machines");
		btnMachines.setBounds(bX, bY + 90, bW, bH);
		btnMachines.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnMachines) {
					if (machineListFrame == null)
						machineListFrame = new MachineListFrame();
					machineListFrame.fillListMachine();
					machineListFrame.setVisible(true);
				}

			}
		});
		pBtn.add(btnMachines);

		btnMachineTypes = new JButton("MachineTypes");
		btnMachineTypes.setBounds(bX, bY + 120, bW, bH);
		btnMachineTypes.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnMachineTypes) {
				}

			}
		});
		pBtn.add(btnMachineTypes);

		btnSpareParts = new JButton("SpareParts");
		btnSpareParts.setBounds(bX, bY + 150, bW, bH);
		btnSpareParts.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnSpareParts) {
				}

			}
		});
		pBtn.add(btnSpareParts);
	}

	private void getIdleTimeFields() {
		JPanel pIdleTime = new JPanel(null);
		pIdleTime.setBounds(200, 0, 280, 210);
		pIdleTime.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED), "Calc-Idle-Time"));
		getContentPane().add(pIdleTime);

		int x = 20;
		int y = 20;
		int w = 160;
		int h = 22;

		lMname = new JLabel("Machine");
		lMname.setBounds(x, y, w, h);
		pIdleTime.add(lMname);

		cbxMachine = new JComboBox(modelMachines);
		cbxMachine.setBounds(x, y + 20, w + 50, h);
		cbxMachine.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == cbxMachine) {
					Machine machine = (Machine) modelMachines.getSelectedItem();
					if (!chbYM.isSelected() && modelMachines.getSize() != 0) {
						// Calculate the idle time
						lResult.setText(machine.getIdleTimeTotalDetails(machine
								.getIdleTimeTotal()));
						info.setText("");
					}
					if (chbYM.isSelected()) {
						try {
							// Get year and month
							int year = Integer.parseInt(txtYM.getText()
									.substring(0, 4));
							int month = Integer.parseInt(txtYM.getText()
									.substring(5, txtYM.getText().length()));
							// Calculate the idle time
							lResult
									.setText(machine
											.getIdleTimeTotalDetails(machine
													.getIdleTimeTotal(new GregorianCalendar(
															year, month, 15))));
							info.setText("");
						} catch (NumberFormatException ex) {
							info.setText("Wrong Date format!");
						}
					}
				}
			}
		});
		pIdleTime.add(cbxMachine);

		chbYM = new JCheckBox("Year-Month (Jan->00)");
		chbYM.setBounds(x, y + 50, w - 25, h);
		chbYM.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == chbYM) {
					if (chbYM.isSelected())
						// Able to type the year and the month
						txtYM.setEnabled(true);
					else
						// Unable to type the year and the month
						txtYM.setEnabled(false);
				}
			}
		});
		chbYM.setToolTipText("By typing the year and the month "
				+ "Idle-Time-Calculation" + " will be within that time.");
		pIdleTime.add(chbYM);

		txtYM = new JTextField("yyyy-mm");
		txtYM.setBounds(x + 140, y + 50, w - 90, h);
		txtYM.setEnabled(false);
		pIdleTime.add(txtYM);

		lIdleTime = new JLabel("Idle-Time-For-Chosen-Machine:");
		lIdleTime.setBounds(x, y + 80, w + 50, h);
		pIdleTime.add(lIdleTime);

		lResult = new JLabel("<>");
		lResult.setBounds(x, y + 100, w + 100, h);
		pIdleTime.add(lResult);

		lTotal = new JLabel("Idle-Time-For-All-Machine:");
		lTotal.setBounds(x, y + 130, w + 100, h);
		pIdleTime.add(lTotal);

		lFullResult = new JLabel("<>");
		lFullResult.setBounds(x, y + 150, w + 100, h);
		pIdleTime.add(lFullResult);
	}

	/**
	 * Fills the ComboBox with Machines, and calculates the Total IdleTime for
	 * all Machines.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private void fillComboBoxModel() {
		long maxIdleTime = 0;
		for (Machine m : Service.getService().getMachines()) {
			modelMachines.addElement(m);
			maxIdleTime += m.getIdleTimeTotal();
		}
		if (!Service.getService().getMachines().isEmpty()) {
			lFullResult.setText(Service.getService().getMachines().get(0)
					.getIdleTimeTotalDetails(maxIdleTime));
		}
	}

	/**
	 * Gets the SimpleDateFormat.
	 * 
	 * @return to the SimpleDateFormat.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public static SimpleDateFormat getSimpleDateFormat() {
		return m_frm;
	}
}
